package com.example.day03.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class LogController {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @GetMapping("demo")
    public String demo() {
        return "hello world";
    }

    /**
     * # 启动zookeeper
     * /export/server/zookeeper/bin/zk.sh start
     * /export/server/zookeeper/bin/zk.sh status
     * # 启动kafka
     * /export/server/kafka/bin/kafka.sh start
     * /export/server/kafka/bin/kafka.sh stop
     * kafka-console-producer.sh --broker-list node1:9092,node2:9092,node3:9092 --topic abc

     *
     * /export/server/bigtimedata
     * -rw-r--r--. 1 root root     1233 Aug 20 23:58 application.yml
     * -rwxr-xr-x. 1 root root 15670411 Aug 20 22:33 gmall2020-mock-log-2021-10-10.jar
     * -rw-r--r--. 1 root root     1143 Aug 20 22:29 logback.xml
     * -rw-r--r--. 1 root root      694 Aug 20 22:29 path.json
     *
     *
     * https://www.bilibili.com/video/BV1HU4y1T73o?p=25&vd_source=2206412ce4975125b2458c9d1ed874c7
     * 通过mock自动发送数据
     * vim application.yml
     * java -jar gmall2020-mock-log-2021-10-10.jar
     *
     * kafka-topics.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --list
     * kafka-topics.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --delete --topic abc
     * kafka-topics.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --create --topic abc
       * kafka-console-producer.sh --broker-list node3:9092 --topic test-topic
       * kafka-console-consumer.sh --bootstrap-server node1:9092,node2:9092,node3:9092 --topic abc --from-beginning
     * kafka-leader-election.sh --bootstrap-server node3:9092 --topic abc --election-type preferred --all-topic-partitions
     * @param JsonStr
     * @return
     */

    @RequestMapping("applog") // http://192.168.1.4:8090/applog?param=123
    public String applog(@RequestParam("param") String JsonStr)
    {
        kafkaTemplate.send("test-topic", JsonStr);
        System.out.println(JsonStr);
        return "hello world";
    }
}
